package com.studyyoun.android.nonghe.find;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.PullRecyclerViewUtils;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewLinserner;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewOnItemClickLinserner;
import com.androidlongs.pullrefreshrecyclerylib.model.PullRecyclerMoreStatueModel;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.ArtDetaileActivity;
import com.studyyoun.android.nonghe.home.activity.VideoDetaileActivity;
import com.studyyoun.android.nonghe.home.model.ArtModel;
import com.studyyoun.android.nonghe.home.model.OneClassModel;
import com.studyyoun.android.nonghe.home.model.TwoClassModel;
import com.studyyoun.android.nonghe.home.model.VideoModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by androidlongs on 2017/11/3.
 */

public class WorkClassDetailsActivity extends CommonBaseActivity {
    private List<OneClassModel> mOneClassModelList = new ArrayList<>();
    private String mOneClassId;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

        mOneClassId = intent.getStringExtra("oneClassId");
        if (TextUtils.isEmpty(mOneClassId)) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    WorkClassDetailsActivity.this.finish();
                }
            }, 2000);

            ToastUtils.show("数据异常 请重新进入 ", mContext);
            return;
        }


    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_work_class_details_layout;
    }

    private LinearLayout mContentLayout;
    private PullRecyclerViewUtils mPullRecyclerViewUtils;
    private List<PullRecyclerMoreStatueModel> mCommonClassModellList = new ArrayList<>();

    @Override
    protected void commonInitView(View view) {

        TextView titleTextView = findViewById(R.id.tv_base_header_title);
        titleTextView.setText("媒体详情");
        findViewById(R.id.ll_base_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WorkClassDetailsActivity.this.finish();
            }
        });

        mContentLayout = findViewById(R.id.ll_work_main_content);
        //初始化
        mPullRecyclerViewUtils = PullRecyclerViewUtils.getInstance();

        //3初始化Recyclerview
        /**
         * 参数一 this Context实例
         * 参数二 单一布局模式条目布局ID
         * 参数三 数据集合
         * 参数四 回调监听
         */
        RelativeLayout relativeLayout = mPullRecyclerViewUtils.setRecyclerViewFunction(mContext, -1, mCommonClassModellList, mPullRecclerLinserner, mPullRecyclerViewOnItemClickLinserner);

        //4相关设置

        //下拉刷新和上拉加载更多模式
        mPullRecyclerViewUtils.setRecyclerviewStatue(PullRecyclerViewUtils.RECYCLRYVIEW_STATUE.PULL_AND_UP);

        mPullRecyclerViewUtils.setFirstDefaultPageType(PullRecyclerViewUtils.SHOW_DEFAUTLE_PAGE_TYPE.LOADING);

        mPullRecyclerViewUtils.setPullScrollRecyclerLiserner(mOnPullRecyclerviewScrollLiserner);
        //5将recyclerview添加到当前显示的页面中
        mContentLayout.addView(relativeLayout);
    }

    @Override
    protected void commonFunction() {

    }

    @Override
    protected void commonDelayFunction() {

        mPullRecyclerViewUtils.startLoadingAnimation();
        loadingClassDetails();
    }

    private void loadingClassDetails() {
        LogUtils.d("加载分类 详情" + HttpUrlHelper.sGetWorkAndMediaAllClassUrl + "?mOneClassId=" + mOneClassId);

        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("oneClassId", mOneClassId);

        OkhttpCommonClient.sentPostRequest(HttpUrlHelper.sGetWorkAndMediaAllClassUrl, key, new OkhttpCommonCallBack(OneClassModel.class) {
            @Override
            public void onSuccess(Object obj) {


                mPullRecyclerViewUtils.stopLoadingAnimation();

                if (obj != null) {
                    List<OneClassModel> lOneClassModelList = (List<OneClassModel>) obj;
                    if (lOneClassModelList.size() > 0) {
                        OneClassModel lOneClassModel = lOneClassModelList.get(0);
                        List<TwoClassModel> lTwoClassModels = lOneClassModel.getTwoClassModels();

                        if (lTwoClassModels != null) {
                            List<TwoClassModel> newList = new ArrayList<>();
                            for (TwoClassModel lTwoClassModel : lTwoClassModels) {
                                if (lTwoClassModel.getIsShow() == 1) {
                                    newList.add(lTwoClassModel);
                                }
                            }


                            for (int lI = 0; lI < newList.size(); lI++) {
                                TwoClassModel lTwoClassModel = newList.get(lI);
                                PullRecyclerMoreStatueModel lPullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                                lPullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_work_class_des_layout;
                                lPullRecyclerMoreStatueModel.itemType = 1;
                                lPullRecyclerMoreStatueModel.model = lTwoClassModel;

                                mCommonClassModellList.add(lPullRecyclerMoreStatueModel);
                            }
                            mPullRecyclerViewUtils.setLoadingDataList(mCommonClassModellList);
                        } else {
                            mPullRecyclerViewUtils.setLoadingDataList(mCommonClassModellList);
                        }

                    } else {
                        mPullRecyclerViewUtils.setLoadingDataList(mCommonClassModellList);
                    }


                } else {
                    ToastUtils.show("暂无数据", mContext);
                }
            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("加载异常", mContext);
                mPullRecyclerViewUtils.setLoadingDataList(mCommonClassModellList);
                mPullRecyclerViewUtils.stopLoadingAnimation();
            }
        });
    }

    private PullRecyclerViewLinserner mPullRecclerLinserner = new PullRecyclerViewLinserner() {
        @Override
        public void loadMoreData() {
            mPullRecyclerViewUtils.setLoadingDataList(null);
        }

        @Override
        public void loadingRefresDataFunction() {
            mCommonClassModellList.clear();
            loadingClassDetails();
        }

        @Override
        public void setViewDatas(View itemView, int position, int itemType, Object object) {

            PullRecyclerMoreStatueModel lPullRecyclerMoreStatueModel = (PullRecyclerMoreStatueModel) object;
            itemType = lPullRecyclerMoreStatueModel.itemType;
            if (itemType == 1) {

                TwoClassModel lTwoClassModel = (TwoClassModel) lPullRecyclerMoreStatueModel.model;

                TextView twoClassTitleTextView = itemView.findViewById(R.id.tv_two_class_title);
                twoClassTitleTextView.setText("#" + (position + 1) + lTwoClassModel.getTwoClassTitle());


                List<ArtModel> lArticleModels = lTwoClassModel.getArticleModels();
                RecyclerView artListRecyclerView = itemView.findViewById(R.id.rv_class_two_art);
                setTwoClassArtListFunction(lArticleModels, artListRecyclerView);

                List<VideoModel> lVideoModels = lTwoClassModel.getVideoModels();
                RecyclerView videoListRecyclerView = itemView.findViewById(R.id.rv_class_two_video);
                setTwoClassVideoListFunction(lVideoModels, videoListRecyclerView);

            }

        }
    };

    private void setTwoClassVideoListFunction(final List<VideoModel> pVideoModels, RecyclerView pVideoListRecyclerView) {
        if (pVideoModels != null && pVideoModels.size() > 0) {

            float recyHeight = pVideoModels.size() * mScaledDensity * 28;

            LinearLayout.LayoutParams lLayoutParams = (LinearLayout.LayoutParams) pVideoListRecyclerView.getLayoutParams();

            if (lLayoutParams != null) {
                lLayoutParams.height = (int) recyHeight;
                pVideoListRecyclerView.setLayoutParams(lLayoutParams);
            }
            pVideoListRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            pVideoListRecyclerView.setVisibility(View.VISIBLE);

            pVideoListRecyclerView.setAdapter(new RecyclerView.Adapter() {
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return new VideoListItemHolder(View.inflate(mContext, R.layout.item_work_class_des_video, null), mContext);
                }

                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

                    VideoListItemHolder lVideoListItemHolder = (VideoListItemHolder) holder;
                    VideoModel lVideoModel = pVideoModels.get(position);
                    lVideoListItemHolder.setDatasFunction(lVideoModel, position);
                }

                @Override
                public int getItemCount() {
                    return pVideoModels.size();
                }
            });

        }else{
            float recyHeight =  mScaledDensity * 28;

            LinearLayout.LayoutParams lLayoutParams = (LinearLayout.LayoutParams) pVideoListRecyclerView.getLayoutParams();

            if (lLayoutParams != null) {
                lLayoutParams.height = (int) recyHeight;
                pVideoListRecyclerView.setLayoutParams(lLayoutParams);
            }
            pVideoListRecyclerView.setVisibility(View.INVISIBLE);
        }
    }

    private void setTwoClassArtListFunction(final List<ArtModel> pArticleModels, RecyclerView pArtListRecyclerView) {
        if (pArticleModels != null && pArticleModels.size() > 0) {

            float recyHeight = pArticleModels.size() * mScaledDensity * 28;

            LinearLayout.LayoutParams lLayoutParams = (LinearLayout.LayoutParams) pArtListRecyclerView.getLayoutParams();

            pArtListRecyclerView.setVisibility(View.VISIBLE);
            if (lLayoutParams != null) {
                lLayoutParams.height = (int) recyHeight;
                pArtListRecyclerView.setLayoutParams(lLayoutParams);
            }
            pArtListRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            pArtListRecyclerView.setAdapter(new RecyclerView.Adapter() {
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return new ArtListItemHolder(View.inflate(mContext, R.layout.item_work_class_des_art, null), mContext);
                }

                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

                    ArtListItemHolder lArtListItemHolder = (ArtListItemHolder) holder;
                    ArtModel lArtModel = pArticleModels.get(position);
                    lArtListItemHolder.setDatasFunction(lArtModel, position);
                }

                @Override
                public int getItemCount() {
                    return pArticleModels.size();
                }
            });

        }else{
            float recyHeight =  mScaledDensity * 28;

            LinearLayout.LayoutParams lLayoutParams = (LinearLayout.LayoutParams) pArtListRecyclerView.getLayoutParams();

            if (lLayoutParams != null) {
                lLayoutParams.height = (int) recyHeight;
                pArtListRecyclerView.setLayoutParams(lLayoutParams);
            }
            pArtListRecyclerView.setVisibility(View.INVISIBLE);
        }
    }

    private static class ArtListItemHolder extends RecyclerView.ViewHolder {

        private final TextView mTitleTextView;
        private Context mContext;

        public ArtListItemHolder(View itemView, Context pContext) {
            super(itemView);
            mContext = pContext;
            mTitleTextView = itemView.findViewById(R.id.tv_item_class_title);
        }

        public void setDatasFunction(final ArtModel pArtModel, int pPosition) {
            mTitleTextView.setText("" + (pPosition + 1) + " " + pArtModel.getArtName());

            mTitleTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ArtDetaileActivity.class);
                    intent.putExtra("artModelId", String.valueOf(pArtModel.getId()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    private static class VideoListItemHolder extends RecyclerView.ViewHolder {

        private final TextView mTitleTextView;
        private Context mContext;

        public VideoListItemHolder(View itemView, Context pContext) {
            super(itemView);
            mContext = pContext;
            mTitleTextView = itemView.findViewById(R.id.tv_item_class_title);
        }

        public void setDatasFunction(final VideoModel pVideoModel, int pPosition) {
            mTitleTextView.setText("" + (pPosition + 1) + " " + pVideoModel.getTitle());

            mTitleTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, VideoDetaileActivity.class);
                    intent.putExtra("videoId", String.valueOf(pVideoModel.getId()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    private PullRecyclerViewOnItemClickLinserner mPullRecyclerViewOnItemClickLinserner = new PullRecyclerViewOnItemClickLinserner() {
        @Override
        public void setonItemClick(int position, int itemType, Object object) {

        }
    };

    private PullRecyclerViewUtils.OnPullRecyclerviewScrollLiserner mOnPullRecyclerviewScrollLiserner = new PullRecyclerViewUtils.OnPullRecyclerviewScrollLiserner() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy, LinearLayoutManager mLinearLayoutManager) {

        }
    };

}
